﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Lucene.Net.Analysis;

using Document = Lucene.Net.Documents.Document;
using IndexReader = Lucene.Net.Index.IndexReader;
using IndexWriter = Lucene.Net.Index.IndexWriter;
using Term = Lucene.Net.Index.Term;
using TermEnum = Lucene.Net.Index.TermEnum;
using StandardAnalyzer = Lucene.Net.Analysis.Standard.StandardAnalyzer;

namespace OgilvyOne.Utility
{
    /// <summary>
    /// 全文搜索
    /// </summary>
    public class LuecenSearchUtil
    {
        private LuecenSearchUtil(string spath)
        {
            this.indexpath = spath + indexpath;
        }

        private const System.String cnindex = "cnindex";
        private string indexpath = "index";

        public static LuecenSearchUtil GetLuecenSearchUtil(string type, string serverpath)
        {
            return new LuecenSearchUtil(serverpath);
        }
        public static string GetType(string layerid)
        {
            return "cn";
        }


        public IndexWriter CreateIndexSearch(bool create)
        {
            FileInfo root = new FileInfo(this.indexpath);
            return new IndexWriter(cnindex, GetAnalyzer(), create);
        }

        public void AddDoc(IndexWriter indexWriter, )
        {

        }

        protected Analyzer GetAnalyzer()
        {
            return new StandardAnalyzer();
        }

        protected void delete(string id)
        {

        }

        private void IndexDocs(System.IO.FileInfo file, IndexReader reader, bool create)
        {
            if (!create)
            {
                // incrementally update

                IndexDocs(file);


                //Term term = new Term("id", id);
                   
                //reader.DeleteDocuments(uidIter.Term());


  
                reader.Close(); // close existing index
            }
            // don't have exisiting
            else
                IndexDocs(file);
        }

        private void IndexDocs(System.IO.FileInfo file)
        {
            
            // creating a new index
            Document doc = GetDocument();
            writer.AddDocument(doc); // add docs unconditionally
              
        }

        private Document GetDocument()
        {
            return null;
        }

    }
}
